package com.reworking.reworking_api.controller;


import com.emallos.xiama.dto.MemberDto;
import com.emallos.xiama.model.MemberModel;
import com.emallos.xiama.service.MemberService;
import com.emallos.xiama.support.controller.RestSupportController;

import com.emallos.xiama.support.repository.PageResult;
import com.emallos.xiama.support.vo.RetModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Valid;
import java.util.concurrent.ExecutionException;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.math.BigDecimal;

/**
 * @author cnlinjie@foxmail.com
 *  ,控制类
 */
@Validated
@RestController
@RequestMapping("/member")
public class MemberController extends RestSupportController {

private final static Logger logger = LoggerFactory.getLogger(MemberController.class);


@Autowired
private MemberService service;

    // 增加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public RetModel save (
    @Valid @RequestBody
        MemberDto dto) {
        Long id = service.save(dto);
        return RetModel.get().put("id",id);
     }

    // 更新
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public RetModel update(
    @PathVariable("id")  Long id,
    @Valid @RequestBody
        MemberDto dto
    ) {
        dto.setId(id);
        service.update(dto);
        return RetModel.get();
    }

    // 删除
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RetModel del(
    @PathVariable("id")
                Long id
    ) {
        service.deleteById(id);
        return RetModel.get();
     }


    // 列表
    @RequestMapping(value = "", method = RequestMethod.GET)
    public RetModel page(
    @RequestParam(defaultValue = "1")
            Integer pageIndex,
    @RequestParam(defaultValue = "10")
            Integer pageSize,
            String keyword, // 关键字
            String wxOpenId  , //   openid
            String wxUnionId  , //   unionid
            String wxHeadImg  , //   头像
            String wxNickname  , //   昵称
            String realname  , //   真实姓名
            String gender  , //   性别
            String phone  , //   手机号
            Integer age  , //   年龄
            String sosRealname  , //   紧急联系人
            String sosPhone  , //   紧急联系电话
            String cardId  , //   身份号码
            String birthday  , //   生日
            String cardType  , //   证件类型
            String clothesSize  , //   衣服大小
            Integer depositPayStatus  , //   押金支付状态 （0 未支付，1 已支付）
            BigDecimal donationTotal  , //   捐款总数
            BigDecimal bringDonationTotal  , //   捐款总数
            Integer unreadMessage  , //   未读消息
            Integer unoperMessage  , //   未操作消息
            Integer fromSource  , //   添加方式，默认0 -> 自动注册，1 -> 手动添加，2 -> 导入 
            Date lastMessage  , //   最后查看消息时间
            String note  , //   备注
            String org   //   组织
    ) {
        MemberDto dto = new MemberDto(pageIndex,pageSize);
        PageResult<MemberModel> page = service.page(dto);
        return RetModel.get().setData(page);
    }

    // 单个
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RetModel get(
    @PathVariable("id")
    Long id ) {
        MemberModel model = service.getById(id);
        return RetModel.get().setData(model);
    }

}
